<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    :width="width"
    append-to-body
  >
    <el-form ref="form" label-width="auto" v-if="showCurrentTag">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item :label="currentLabel">
            <div class="empty" v-if="!currentTagList || currentTagList.length == 0">暂无，请从下方选择并绑定</div>
            <el-tag
              :key="item.id"
              v-for="item in currentTagList"
              closable
              :disable-transitions="false"
              @close="handleDel(item)">
              {{item.realName}}
            </el-tag>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <Table 
      :tableData="tableData" 
      :openHighlight="true" 
      @handleCurrentChange="handleCurrentChange" 
      ref="table"
    />

    <pagination
      v-show="tableData.total > 0"
      :total="tableData.total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      layout='total, prev, pager, next'
      @pagination="getList"
      :autoScroll="false"
    />

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">绑 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "",
  props: {
    tableFrame: {
      type: Array,
      default: () => []
    },
    title: {
      type: String,
      default: "绑定"
    },
    width: {
      type: String,
      default: "800px"
    },
    currentLabel: {
      type: String,
      default: "当前绑定"
    },
    showCurrentTag: {
      type: Boolean,
      default: true
    },
    delSucessLabel: {
      type: String,
      default: "解绑成功"
    },
    bindSucessLabel: {
      type: String,
      default: "绑定成功"
    },
    delClose: {
      type: Boolean,
      default: false
    },
    bindClose: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      originData: {}, // 记录弹窗的原始数据
      queryParams: {
        pageNum: 1,
        pageSize: 5
      },
      tableData: {
        loading: false,
        columns: [],
        total: 0,
        data: []
      },
      currentDataId: "",
      currentTagList: [],
      chosedRow: "",
      visible: false
    };
  },

  methods: {
    showDialog(data){
      this.reset();
      this.tableData.data = []
      this.tableData.total = 0
      this.tableData.columns = this.tableFrame
      this.queryParams.pageNum = 1
      this.currentDataId = data.id
      this.originData = data;
      this.getTagList(data)
      this.getList(data)
      this.visible = true;
    },

    getTagList(data){
      if(!this.showCurrentTag){
        return;
      }
      this.$emit("getTagList", data, (res) => {
        if(res.rows){
          this.currentTagList = res.rows
        }
      })
    },

    getList(){
      this.loading = false;
      this.$emit("getList", this.originData, this.queryParams, (res) => {
        if(res.rows){
          this.tableData.data = res.rows;
          this.tableData.total = res.total;
          this.tableData.loading = false;
        }
      })
    },

    handleCurrentChange(row){
      this.chosedRow = row
    },

    handleDel(data){
      this.$emit("delTag", data, (res) => {
        if(this.delClose){
          this.visible = false;
        }else{
          this.$modal.msgSuccess(this.delSucessLabel);
          const dataId = {id: this.currentDataId}
          this.getTagList(dataId)
          this.getList(dataId)
        }
      })
    },

    submitForm() {
      if(!this.chosedRow){
        return;
      }
      this.$emit("bindTag", {
        currentId: this.currentDataId,
        chosedRow: this.chosedRow
      }, () => {
        if(this.bindClose){
          this.$modal.msgSuccess(this.bindSucessLabel);
          this.visible = false;
        }else{
          this.$modal.msgSuccess(this.bindSucessLabel);
          const dataId = {id: this.currentDataId}
          this.getTagList(dataId)
          this.getList(dataId)
        }
      })
    },

    cancel() {
      this.visible = false;
      this.reset();
    },

    reset() {
      this.currentTagList = []
      this.chosedRow = ""
      this.currentDataId = ""
      this.resetForm("form");
      this.$forceUpdate();
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-upload-list__item{
  width: 100px;
  height: 100px;
}

::v-deep .el-upload--picture-card{
  width: 100px;
  height: 100px;
  line-height: 100px;
}

::v-deep .el-tag{
  margin-right: 10px;
}
</style>
